//
//  MyorderViewController.m
//  parent
//
//  Created by yanzhen on 15/7/19.
//  Copyright (c) 2015年 ci123. All rights reserved.
//

#import "MyorderViewController.h"
#import "AFAppClient.h"
#import "OrderTableViewCell.h"
#import "MyorderdetailViewController.h"
#import "Config.h"

@interface MyorderViewController ()
@property (weak, nonatomic) IBOutlet UITableView *tableview;
@property NSArray *data;
@end

@implementation MyorderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"我的订单";
    [self CustomBackButton];
    [self initData];
}
-(void)initData
{
    [self showHud:kLoadingData isDim:YES];
    [[AFAppClient sharedManager]getWithURL:[NSString stringWithFormat:@"%@%@",SERVER_URL_NEW,kOrderList] withParameters:@{@"uid":@([[Config Instance]getUID])} success:^(NSDictionary *responseObject) {
        NSUInteger code = [[responseObject objectForKey:@"code"]integerValue];
        if(code==0){
            
            [self showHudComplete:@"暂无订单" success:YES];
        }else if (code ==1){
            [self hideHud2];
            _data = [responseObject objectForKey:@"result"];
            [_tableview reloadData];
        }
    } failure:^(NSError *error) {
        [self showHudComplete:kNoNetwork success:NO];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma TableView 相关

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01f;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"OrderTableViewCell";
    OrderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray *nibArr = [[NSBundle mainBundle]loadNibNamed:@"OrderTableViewCell" owner:self options:nil];
        cell = (OrderTableViewCell *)[nibArr objectAtIndex:0];
        [cell setAccessoryType:UITableViewCellAccessoryNone];
    }
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    NSDictionary *article;
    article = [_data objectAtIndex:indexPath.row];
    
    
    NSString *title = [article objectForKey:@"goods_name"];
    cell.goods_name.text = title;
    
    NSString *goods_price = [article objectForKey:@"goods_price"];
    cell.goods_price.text = goods_price;
    
    NSString *order_num = [article objectForKey:@"order_num"];
    cell.order_num.text = order_num;
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%@",indexPath);
    //该方法响应列表中行的点击事件
    NSInteger row = indexPath.row;
    NSDictionary *dict= [_data objectAtIndex:row];
    
    NSUInteger pid = [[dict objectForKey:@"id"]integerValue ];
    MyorderdetailViewController *detail = [[MyorderdetailViewController alloc]init];
    detail.oid =pid;
    detail.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:detail animated:YES];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [_data count];
}

//设置Cell行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 99;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
